package com.huatai.common.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import java.nio.charset.StandardCharsets;

/**
 * 安全相关工具类
 *
 * @author zhangyh
 */
public class EncryptUtils {

    /**
     * 公钥
     */
    public static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrhvndgpB+ICtB8c2jxZZ4IO6l\n" +
		"RJCvNooYMkTjVN7klyC3fZZqaJ12rd9X92ceA1+9KDOp/udbPOyc1a+XnbQK2DPK\n" +
		"ww2el+X8HNNypH0e59OoujGsz/oxNPshY4NhBV2L6j4HO0d/PxpZ/Y/xznTvBl/E\n" +
		"vSxSIRKYT3Fu49lZ2QIDAQAB";

    /**
     * 私钥
     */
    public static final String PRIVATE_KEY = "MIICXQIBAAKBgQCrhvndgpB+ICtB8c2jxZZ4IO6lRJCvNooYMkTjVN7klyC3fZZq\n" +
		"aJ12rd9X92ceA1+9KDOp/udbPOyc1a+XnbQK2DPKww2el+X8HNNypH0e59OoujGs\n" +
		"z/oxNPshY4NhBV2L6j4HO0d/PxpZ/Y/xznTvBl/EvSxSIRKYT3Fu49lZ2QIDAQAB\n" +
		"AoGAb90+VYVSIL9wFrjRCS7SfBPWL7hwt6RVGYSnQq9WEjSxpJlmII2LrBunHiJ+\n" +
		"gKdop99xGjvgvLMWCQ6fTr0N7jbAOMGid6rj5/bHCV0FSCT0o1BBLUNdUaLc06iF\n" +
		"zJtgy9eUTaojlSpKcYEbG5VRwNNaYdr9D+1mPfpkhKz2vMUCQQDfdW9UZPi0coyA\n" +
		"oS0rNdDG43BljdMsJ+uFnGEmtIQLhQHP078U5lyjePP368k1saKFispkoOHXLFF4\n" +
		"e1SFlR7vAkEAxIGHs7YNA8FUxdX4qci+pCcvRmlxW71NgBXx1DvG01MVq3atxCez\n" +
		"DZQLA2dJ2q6ZGOA1BHaygCPXWJvGIlOTtwJBAMmGP0D9Ts5uAAiBUxsFDaRQuazc\n" +
		"mvXXirCUt4kQg4VgDqI/dLSEjEdhbaTJIE0sRkXpRV47OXG0pkTh9PgD5EECQQCD\n" +
		"djSW4cArNvfDsrY8v/PqVdymunv/59ElU4kJd0Q4tGjW4Pq3M+fxuYz0pfEev5fk\n" +
		"sHpHljEs0M2MlgojhM5ZAkA/DhHiKhrN79tQLVQYz38Ztg7t4Xqi5sdXw50hgKtM\n" +
		"H1czf45dMFZXoGIPD0qC/MraqyfKfcMqps4nuexGZWeJ";

    /**
     * rsa公钥加密
     *
     * @param data      待加密数据
     * @return 加密后字符串, 采用Base64编码
     */
    public static String encryptByRsa(String data) {
        RSA rsa = SecureUtil.rsa(null, PUBLIC_KEY);
        return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey);
    }

    /**
     * rsa公钥加密
     *
     * @param data      待加密数据
     * @return 加密后字符串, 采用Hex编码
     */
    public static String encryptByRsaHex(String data) {
        RSA rsa = SecureUtil.rsa(null, PUBLIC_KEY);
        return rsa.encryptHex(data, StandardCharsets.UTF_8, KeyType.PublicKey);
    }

    /**
     * rsa私钥解密
     *
     * @param data       待解密数据
     * @return 解密后字符串
     */
    public static String decryptByRsa(String data) {
        RSA rsa = SecureUtil.rsa(PRIVATE_KEY, null);
        return rsa.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8);
    }

}
